<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

$GLOBALS['_XLANG'] = array();

function otx_language_name($language_name = null)
{
	if (null === $language_name) {
		return otx_param_string('_language_name');
	}
	otx_param_set('_language_name', $language_name);
}

function otx_language_path($language_name, $sub_path = null)
{
	$language_path = DIR_LANGUAGES . $language_name . DS;

	if (empty($sub_path)) {
		return $language_path;
	} else {
		return $language_path . $sub_path;
	}
}

function otx_language_instance($language_name)
{
	static $languages = null;

	if (null === $languages) {
		$languages = otx_cache_fetch('languages');
		if (false === $languages) {
			$languages = array();

			$sql = 'SELECT * FROM #system_language#';
			$rows = otx_db_fetch_all($sql);

			if ($rows) {
				foreach ($rows as $row) {
					$languages[$row['language_name']] = $row;
				}
			}

			otx_cache_write('languages', $languages);
		}
	}

	if (isset($languages[$language_name]) && isset($languages[$language_name]['instance'])) {
		return $languages[$language_name]['instance'];
	}

	$language_path = otx_language_path($language_name);
	if (!is_dir($language_path)) {
		throw new Exception('Language directory not exists: ' . $language_path);
	}

	$language_file = $language_path . DS. $language_name . '.php';
	if (!is_file($language_file)) {
		throw new Exception('Language file not exists: ' . $language_file);
	}

	require_once $language_file;

	$language_class = $language_name . '_language';
	if (!class_exists($language_class)) {
		throw new Exception('Language class not exists: ' . $language_class);
	}

	$instance = new $language_class();
	$instance->language_name = $language_name;
	$instance->language_path = $language_path;

	if (isset($languages[$language_name])) {
		$instance->language_id = $languages[$language_name]['language_id'];
	}

	$languages[$language_name]['instance'] = $instance;
	return $instance;
}

function otx_language_info($language_name)
{
	if (is_string($language_name)) {
		$instance = otx_language_instance($language_name);
	} else {
		$instance = $language_name;
	}

	$info = array(
			'name' => $instance->language_name,
			'title' => $instance->language_name,
			'description' => null,
			'website' => null,
			'email' => null,
			'author' => null,
			'version' => null
	);

	return array_merge($info, $instance->get_info());
}

function otx_language_load($ext_type, $ext_name, $file_name, $merge = true)
{
	global $_XLANG;
	$_ = array();

	if ('plugin' == $ext_type) {
		$language_dir = otx_plugin_path($ext_name, 'languages/');
	} else if ('language' == $ext_type) {
		$language_dir = otx_language_path($ext_name, 'languages/');
	} else {
		$language_dir = otx_module_path($ext_name, 'languages/');
	}

	if (otx_language_name() != 'en_US') {
		$language_file = $language_dir . 'en_US/' . $file_name . '.lng';
		if (file_exists($language_file)) {
			include $language_file;
		}
	}

	$language_file = $language_dir . otx_language_name() . '/' . $file_name . '.lng';
	if (file_exists($language_file)) {
		include $language_file;
	}

	if ($merge) {
		$_XLANG = array_merge($_XLANG, $_);
	}

	if (isset($_['js'])) {
		foreach ($_['js'] as $key => $text) {
			otx_html_js_text($key, $text);
		}
	}

	return $_;
}

function otx_language_parse($key, $args = array())
{
	global $_XLANG;

	if (empty($_XLANG[$key])) {
		return $key;
	}

	$text = $_XLANG[$key];

	foreach ((array) $args as $key => $value) {
		$text = str_replace('{' . $key . '}', $value, $text);
	}

	return $text;
}